<template>
    <div>
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
                <van-field v-model="phone" name="password" label="手机号" placeholder="手机号"
                    :rules="[{ required: true, message: '请填写手机号' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    立即登录
                </van-button>
            </div>
            <div style="margin: 16px;">
                <van-button round block type="primary" @click="parten">
                    立即注册
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { loginApi } from '@/api/api'
import { useRoute } from 'vue-router';
import { showSuccessToast,showFailToast } from 'vant';
const username = ref('')
const password = ref('')
let route = useRoute()
let phone=ref('')
let parten=/^1[3-9]\d{9}/
// loginApi().then((res=>{
//     if(password.value>8){
//         showFailToast('不可大于8位')
//         password.value=''
//     }
//     else{

//     }
// }))
const onSubmit = () => {
    //  if(password.value.length>8){
    //     showFailToast('不可大于8位')
    //      password.value=''
    //  }
    //  else{
   
           loginApi(username.value, password.value).then((res => {
        if(res.data.code==0){
            showSuccessToast('正确')
        console.log('登录', res);

        route.push('/home/cart')
        localStorage.setItem('token', res.data.data.token)
        localStorage.setItem('username', res.data.data.username)
        }
        else{
            showFailToast('错误')
        }

    }))
    }
 

// }
</script>

<style lang="scss" scoped></style>